# coding=utf-8
# 超时参数和重复请求
import requests
from retrying import retry

headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36"}


@retry(stop_max_attempt_number=3)  # 最大重复次数为3次，超过3次报错
def _parse_url(url):
    print("*" * 50)
    response = requests.get(url, headers=headers, timeout=3)  # timeout超时，超过3秒报错
    assert response.status_code == 200
    return response.content.decode()


def parse_url(url):
    try:
        html_str = _parse_url(url)

    except Exception as e:
        print(e)
        html_str = None

    return html_str


if __name__ == '__main__':
    # url = "https://www.baidu.com"
    url = "www.baidu.com"
    resp = _parse_url(url)
    print(resp)
